# -*- coding:utf-8 -*-

import pymysql
import tornado.web


class PymysqlException():

    def default(self):
        return [5, 'mysql操作发生异常！']


def mysql_exception(e):
    # msql = PymysqlException()
    return [5, f'mysql操作发生异常！\n{str(e)}']


def tornado_exception(e):
    return [6, str(e)]


def match(e):
    """匹配错误类型"""
    dl = [4, '未知异常，请查看应用日志！']
    type_str = str(type(e))[8:-2]
    if type_str.startswith('pymysql.'):
        dl = mysql_exception(e)
    elif type_str.startswith('tornado.'):
        dl = tornado_exception(e)
    else:
        dl = [950, repr(e)]
    code, msg = dl
    return {
        'code': code,
        'msg': msg
    }


def exception(e):
    return match(e)


